package demo.com.lgx.wechatfloatdemo;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.IFloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.PermissionUtil;
import com.yhao.floatwindow.Screen;
import com.yhao.floatwindow.Util;
import com.yhao.floatwindow.ViewStateListenerAdapter;

import demo.com.lgx.wechatfloatdemo.weghit.ScaleCircleImageView;
import demo.com.lgx.wechatfloatdemo.weghit.SpecificPositionWebView;

/**
 * Created by Harry on 2018/8/8.
 * desc:
 */

public class WebViewActivity extends BaseActivity {

    private ProgressBar progressBar;
    private String url = "https://blog.csdn.net/HarryWeasley/article/details/73692467";
    //    private String url = "https://www.baidu.com";
    private ScaleCircleImageView myImageView;
    private SpecificPositionWebView webView;
    private FrameLayout parent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        myImageView = findViewById(R.id.image_view);
        progressBar = findViewById(R.id.progress);
        webView = findViewById(R.id.web_view);
        parent = findViewById(R.id.frame_layout);
        Button back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.GONE);
                int scrollY = MyApplication.getMyApplication().getScrollY();
                if (scrollY != 0) {
                    webView.scrollTo(0, scrollY);
                }

            }
        });

        webView.setOnScrollChangedCallback(new SpecificPositionWebView.OnScrollChangedCallback() {
            @Override
            public void onScroll(int l, int t, int oldl, int oldt) {
                MyApplication.getMyApplication().setScrollY(t);
            }
        });

    }


    private void startAnimation(IFloatWindow old) {
        //创建当前视图的bitmap
        View view = parent;
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        myImageView.createAnmiationParam()
                   .setFromLeftX(0)
                   .setToLeftX(old.getBuilder().xOffset)
                   .setFromRightX(parent.getWidth())
                   .setToRightX(old.getBuilder().xOffset + old.getBuilder().mWidth)
                   .setFromTopY(0)
                   .setToTopY(old.getBuilder().yOffset)
                   .setFromBottomY(parent.getHeight())
                   .setFromRadius(0)
                   .setToRadius(old.getBuilder().mWidth / 2)
                   .setToBottomY(old.getBuilder().yOffset + old.getBuilder().mWidth);
        myImageView.startAnimation(bitmap, old.getBuilder().mWidth);
        webView.setVisibility(View.GONE);
        myImageView.setScaleCircleListener(new ScaleCircleImageView.ScaleCircleListener() {
            @Override
            public void onAnimationEnd() {
                finish();
            }

        });
    }

    private void startEmptyAnimation() {
        //创建当前视图的bitmap
        View view = parent;
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        int mWidth = Util.dp2px(this, 75);
        int xOffset = Util.getScreenWidth(this) - mWidth;
        int yOffset = parent.getHeight() / 3;
        myImageView.createAnmiationParam()
                   .setFromLeftX(0)
                   .setToLeftX(xOffset)
                   .setFromRightX(parent.getWidth())
                   .setToRightX(xOffset + mWidth)
                   .setFromTopY(0)
                   .setToTopY(yOffset)
                   .setFromBottomY(parent.getHeight())
                   .setFromRadius(0)
                   .setToRadius(mWidth / 2)
                   .setToBottomY(yOffset + mWidth);
        myImageView.startAnimation(bitmap, mWidth);
        webView.setVisibility(View.GONE);
        myImageView.setScaleCircleListener(new ScaleCircleImageView.ScaleCircleListener() {
            @Override
            public void onAnimationEnd() {
                MyApplication.getMyApplication().setBackNoPermission(true);
                finish();
            }

        });
    }
}
